package com.rockcent.mall.controller;

import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.mall.Routes;
import com.rockcent.mall.lib.domain.MarketMerchant;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.mall.lib.helper.BaseUserHelper;
import com.rockcent.mall.lib.repository.MarketMerchantRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by DK on 16/9/12.
 * 分销市场
 */
@RestController
public class MarketMerchantController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(MarketMerchantController.class);

    @Autowired
    private BaseUserHelper userHelper;

    @Autowired
    private MarketMerchantRepository marketMerchantRepository;

    /**
     * @api {post} /market/merchant/protocol/sign 分销市场 协议签订
     * @apiGroup MARKET_MERCHANT
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *      "code": "1",
     *      "desc": "成功"
     * }
     *
     */
    @RequestMapping(value = Routes.MARKET_MERCHANT_PROTOCOL_SIGN, method = RequestMethod.POST)
    public JsonResult protocolSign(HttpServletRequest request){
        UserInfoDto userInfoDto = userHelper.getSession(request);
        RestDoing doing = jsonResult -> {
            MarketMerchant marketMerchant = marketMerchantRepository.findByMerchantIdAndMallId(userInfoDto.merchant.getId(), userInfoDto.mall.getId());
            if (marketMerchant == null){
                marketMerchant = new MarketMerchant();
                marketMerchant.setMallId(userInfoDto.mall.getId());
                marketMerchant.setMerchantId(userInfoDto.merchant.getId());
            }
            marketMerchant.setSaleProtocolStatus(true);
            marketMerchantRepository.save(marketMerchant);

        };
        return doing.go(request, log);
    }

    /**
     * @api {post} /market/merchant/protocol/judgement 分销市场 协议判断
     * @apiGroup MARKET_MERCHANT
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *      "code": "1",
     *      "desc": "成功"
     *      "data":{
     *          "id":1
     *          "mallId":2
     *          "merchantId":2
     *          "saleProtocolStatus":true
     *      }
     * }
     *
     */
    @RequestMapping(value = Routes.MARKET_MERCHANT_PROTOCOL_JUDGEMENT, method = RequestMethod.GET)
    public JsonResult protocolJudgement(HttpServletRequest request){
        UserInfoDto userInfoDto = userHelper.getSession(request);
        RestDoing doing = jsonResult -> {
            MarketMerchant marketMerchant = marketMerchantRepository.findByMerchantIdAndMallId(userInfoDto.merchant.getId(), userInfoDto.mall.getId());
            jsonResult.data = marketMerchant;
        };
        return doing.go(request, log);
    }

}
